#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>

int main()
{
    //环境变量的指针声明
    extern char ** environ;
    printf("I am a parent process , pid : %d\n",getpid());
    pid_t id = fork();
    if(id == 0)
    {
        printf("I am a child process , pid : %d\n",getpid());
        char *const env_[] = {(char*)"MYPATH = This is MYPATH!",NULL};
        //e:添加环境变量给目标进程，是覆盖式的（0），还是新增式的（1）？0
        execl("./mycmd","mycmd",NULL,environ);
        //execle("./mycmd","mycmd",NULL,env_);

        //execl("./mycmd","mycmd",NULL);
        //execl("/home/hy/linux_code/linux11/mycmd","mycmd",NULL);
    }
   
    int status = 0;
    int ret = waitpid(id,&status,0);
    if(ret == id)
    {
        printf("parent process wait success!\n");
    }
   
   //printf("I am a process , pid : %d\n",getpid());

   //execl("/usr/bin/ls","ls","-a","-l",NULL);
   ////execl("/usr/bin/top","top",NULL);
   //    printf("false\n");

   //printf("Completion of execution , pid : %d\n",getpid());
    return 0;
}


//int main()
//{
//    printf("I am parent process , pid : %d\n",getpid());
//    pid_t id = fork();
//    if(id == 0)
//    {
//        //我们想让子进程执行全新的程序，之前是执行父进程的代码片段
//        printf("I am a child process , pid : %d\n",getpid());
//        //execlp("ls","ls","-al",NULL);
//        
//        char *const argv_[]={(char*)"ls",(char*)"-al",NULL};
//        //execv("/usr/bin/ls",argv_);
//        
//        execvp("ls",argv_);
//
//        //execl("/usr/bin/ls","ls","-al",NULL);
//        exit(1); //只要执行了exit,意味着execl系列调用失败了
//    }
//    int status = 0;
//    int ret = waitpid(id,&status,0);
//    if(ret == id)
//    {
//        printf("parent process wait success!\n");
//    }
//    
//    //printf("I am a process , pid : %d\n",getpid());
//
//    //execl("/usr/bin/ls","ls","-a","-l",NULL);
//    ////execl("/usr/bin/top","top",NULL);
//    //    printf("false\n");
//
//    //printf("Completion of execution , pid : %d\n",getpid());
//
//    return 0;
//}
